package controllers

import javax.inject.{Inject, Singleton}

import cn.socialcredits.activity.business.ActivityService
import play.api.mvc.{Action, Controller}
import reactivemongo.bson.BSONObjectID

import scala.concurrent.Future
import scala.util.{Failure, Success}

/**
 * 活动
 * Created by jingyang on 15/7/28.
 */
@Singleton
class ActivityController @Inject()(activityService: ActivityService
                                    ) extends Controller with BaseController {
  logger.info(getClass.getSimpleName + " run")

  def get(activityId: String) = Action.async {
    BSONObjectID.parse(activityId) match {
      case Success(id) =>
        activityService.findOneById(id).map {
          case Some(activity) =>
            val title = activityId
            Ok(views.html.activity.item(title, activity))

          case None =>
            NotFound
        }
      case Failure(e) =>
        Future.successful(NotFound(e.getLocalizedMessage))
    }
  }

  def result(activityId: String, registrationId: String) = Action.async {
    BSONObjectID.parse(registrationId) match {
      case Success(regOid) =>
        activityService.findRegistrationOneById(regOid).map {
          case Some((registration, activity)) =>
            val title = activityId
            Ok(views.html.activity.result(title, registration, activity))

          case None =>
            NotFound
        }
      case Failure(e) =>
        Future.successful(NotFound(e.getLocalizedMessage))
    }
  }

}
